﻿using Autofac;
using Domain;
using Newtonsoft.Json.Serialization;
using Web.Extensions;
using Web.Extensions.Filters;

namespace Web
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers(action =>
            {
                action.Filters.Add<LoggerActionFilter>();
            }).AddNewtonsoftJson(option =>
            {
                option.SerializerSettings.ContractResolver = new DefaultContractResolver();
                option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                option.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
                option.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                option.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            });
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            services.SwaggerSetup();
            // Configure DBContext
            services.AddDbContextSetup(Configuration);
            #region 授权
            //services.AspNetIdentitySetup();
            //// Configure IdentityServer4
            //services.AddIdentityServer4(builder.Configuration)
            //    .AddAspNetIdentity<Account>().AddDeveloperSigningCredential();
            //#endregion

            //#region 认证
            //services.AddId4Authentication(builder.Configuration);
            #endregion

            
        }

        public void ConfigureContainer(ContainerBuilder builder)
        {
        }

        public void Configure(WebApplication app)
        {
            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                //app.UseSwagger();
                //app.UseSwaggerUI();
                app.UseSwaggerConfiguration();
                //app.InitializeIdentityServerDatabase()
                //    .InitializeApplicationDatabase();
            }
            //app.UseIdentityServer();

            //app.UseAuthentication();

            app.UseHttpsRedirection();

            //app.UseAuthorization();

            app.MapControllers();

            app.Run();
        }

    }
}
